package com.hoodbrains.insideloc.shopping;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import android.content.Context;
import android.widget.BaseAdapter;


public class ShoppingList {
	
//	private ShoppingListActivity act;
	/** Is it a good dependence ? */
	private Set<BaseAdapter> adapters;
	
	private List<ShoppingItem> products; 
	
	private static ShoppingList instance;
	
	public final static ShoppingList getInstance() {
	    return ShoppingList.instance;
	}
	
	public static void init(Context ctx) {
		instance = new ShoppingList();
	}
	
	private ShoppingList() {
		adapters = new HashSet<BaseAdapter>();
//			ShoppingListActivity act) {
//		this.act = act;
		products = new ArrayList<ShoppingItem>();
		
	}

	private void notifyAdapters() {
		BaseAdapter[] adaptersArray = adapters.toArray(new BaseAdapter[adapters.size()]);
		
		for (BaseAdapter adapter : adaptersArray) {
			adapter.notifyDataSetChanged();
		}
	}
	
	//Interface candidate
	public void registerAdapter(BaseAdapter adapter) {
		adapters.add(adapter);
	}
	
	//TODO unregister adapter.
	
	//Interface candidate
	public List<ShoppingItem> getItemList() {
		return products;
	}
	
		
	//Interface candidate
	public void toggleGrabbed(ShoppingItem item) {
		item.toggleGrabbed();
//		adapter.notifyDataSetInvalidated();
		notifyAdapters();
	}
	
	public void add(String product, String rayon) {
		products.add(new ShoppingItem(product, rayon));
//		adapter.notifyDataSetChanged();
		notifyAdapters();
	}
	
	public void delete(ShoppingItem item) {
		products.remove(item);
		
//		adapter.notifyDataSetChanged();
		notifyAdapters();
			
	}
}
